package org.linlinjava.litemall.wx.web;

import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallBookBox;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.service.WxBookBoxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("wx/book/box")
public class WxBookBoxController {
    @Autowired
    private WxBookBoxService bookBoxService;

    /**
     * 添加到书箱
     * @param userId
     * @param goodProductId
     * @return
     */
    @PostMapping("/addToBookBox")
    public Object addToBookBox(@LoginUser Integer userId,@RequestBody Integer goodProductId) {
        return bookBoxService.addToBookBox(userId, goodProductId);
    }

    /**
     * 移除书箱中某本书
     * @param bookBoxId
     * @return
     */
    @PostMapping("/delete")
    public Object delete(@RequestBody Integer bookBoxId) {
        bookBoxService.delete(bookBoxId);
        return ResponseUtil.ok();
    }

    /**
     * 多选删除书箱中的书
     * @param bookBoxIds
     * @return
     */
    @PostMapping("/batchDelete")
    public Object batchDelete(@RequestBody Integer[] bookBoxIds) {
        bookBoxService.batchDelete(bookBoxIds);
        return ResponseUtil.ok();
    }

    /**
     * 获取当前用户的书箱信息
     * @param userId
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/queryMyBox")
    public Object queryList(@LoginUser Integer userId,
                            @Sort @RequestParam(required = false,defaultValue = "add_time") String sort,
                            @Order @RequestParam(required = false,defaultValue = "desc") String order) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        return ResponseUtil.okList(bookBoxService.queryList(userId, sort, order));
    }

    /**
     * 更新书箱
     * @param userId
     * @param bookBox
     * @return
     */
    @PostMapping("/update")
    public Object update(@LoginUser Integer userId, @RequestBody LitemallBookBox bookBox) {
        return bookBoxService.update(userId, bookBox);
    }
}
